package com.sengled.cloud.biz.union.mgt.api.controller;

import com.alibaba.fastjson.JSON;
import com.sengled.cloud.biz.union.mgt.common.util.CurrentUserUtil;
import com.sengled.cloud.biz.union.mgt.model.enums.RoleTypeEnum;
import com.sengled.cloud.biz.union.mgt.model.po.Role;
import com.sengled.cloud.biz.union.mgt.model.request.GetRoleListRequest;
import com.sengled.cloud.biz.union.mgt.model.request.RoleEnableMarkRequest;
import com.sengled.cloud.biz.union.mgt.model.request.SaveOrUpdateRoleRequest;
import com.sengled.cloud.biz.union.mgt.model.response.*;
import com.sengled.cloud.biz.union.mgt.model.vo.*;
import com.sengled.cloud.biz.union.mgt.service.sys.IRoleManageService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by ti on 2017/11/6.
 */
@Controller
@RequestMapping(value = "/api/v1")
public class RoleController {

    @Autowired
    private IRoleManageService roleManageService;

    @ApiOperation(value = "测试查询角色请求", notes = "查询角色对象接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    @RequestMapping(value = "/role", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseMessage<QueryRoleWithPage> getRoleList(@RequestBody GetRoleListRequest getRoleListRequest, HttpServletRequest request) {
        Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        ResponseMessage<QueryRoleWithPage> rm = roleManageService.getRoleList(getRoleListRequest,customerId);
        return rm;
    }

    @ApiOperation(value = "测试获取新增角色页面模块复选框以及权限复选框数据请求", notes = "获取新增角色页面模块复选框以及权限复选框数据接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    @RequestMapping(value = "/role/input", method = {RequestMethod.GET})
    @ResponseBody
    public ResponseMessage<List<ModulePermissions>> getInputModulePermissions(){
        ResponseMessage<List<ModulePermissions>> rm = roleManageService.getModulePermissions();
        return rm;
    }

    @ApiOperation(value = "测试获取修改角色页面模块复选框以及权限复选框数据请求", notes = "获取修改角色页面模块复选框以及权限复选框数据接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    //@PreAuthorize("hasRole('ROLE_15_2')")
    @RequestMapping(value = "/role/edit/{id}", method = {RequestMethod.GET})
    @ResponseBody
    public ResponseMessage<RoleEditResponse> getUpdateModulePermissions(@ApiParam(value = "角色ID", required = true) @PathVariable Long id){
        ResponseMessage<RoleEditResponse> rm = roleManageService.getModulePermissionsWithChecked(id);
        return rm;
    }

    @ApiOperation(value = "测试保存角色save请求", notes = "保存角色对象接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    @RequestMapping(value = "/role/save", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseMessage<SaveOrUpdateRoleRequest> saveRole(@RequestBody SaveOrUpdateRoleRequest saveRoleOrUpdateRequest, HttpServletRequest request){
        Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        List<PkModulePermissions> pkModulePermissionsList = saveRoleOrUpdateRequest.getPkModulePermissionsList();
        pkModulePermissionsList =  pkModulePermissionsList.stream().filter(pkModulePermissions -> pkModulePermissions.getModuleId() != null && pkModulePermissions.getPermissionIds().size()>0).distinct().collect(Collectors.toList());
        saveRoleOrUpdateRequest.setPkModulePermissionsList(pkModulePermissionsList);
        ResponseMessage<SaveOrUpdateRoleRequest> rm = roleManageService.saveOrUpdateRoleModulePermissions(saveRoleOrUpdateRequest,customerId);
        return rm;
    }

    @ApiOperation(value = "测试修改角色update请求", notes = "修改角色对象接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    @RequestMapping(value = "/role/update", method = {RequestMethod.PATCH})
    @ResponseBody
    public ResponseMessage<SaveOrUpdateRoleRequest> updateRole(@RequestBody SaveOrUpdateRoleRequest saveRoleOrUpdateRequest,HttpServletRequest request){
        Long customerId = CurrentUserUtil.getCurrentAccountId(request);
        ResponseMessage<SaveOrUpdateRoleRequest> rm = roleManageService.saveOrUpdateRoleModulePermissions(saveRoleOrUpdateRequest,customerId);
        return rm;
    }

    @ApiOperation(value = "测试删除角色请求", notes = "删除角色对象接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    @RequestMapping(value = "/role/{id}", method = {RequestMethod.DELETE})
    @ResponseBody
    public ResponseMessage<Long> deleteRole(@PathVariable Long id){
        ResponseMessage<Long> rm = roleManageService.deleteRole(id);
        return rm;
    }

    @ApiOperation(value = "测试查询角色类型请求", notes = "查询角色类型接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    @RequestMapping(value = "/role/type", method = {RequestMethod.GET})
    @ResponseBody
    public ResponseMessage<Map> getRoleType() {
        ResponseMessage<Map> rm = new ResponseMessage<Map>();
        Map<Integer,String> map = new HashMap<Integer,String>();
        map.put(RoleTypeEnum.ROLE_TYPE_FORMAL.getKey(),RoleTypeEnum.ROLE_TYPE_FORMAL.getValue());
        map.put(RoleTypeEnum.ROLE_TYPE_TEMPLATE.getKey(),RoleTypeEnum.ROLE_TYPE_TEMPLATE.getValue());
        rm.setData(map);
        return rm;
    }

    @ApiOperation(value = "测试启用停用角色请求", notes = "启用停用角色对象接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    @RequestMapping(value = "/role/enableMark/update", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseMessage<Long> updateEnableMark(@ApiParam(value = "启用标识", required = true) @RequestBody RoleEnableMarkRequest roleEnableMarkRequest){
        ResponseMessage<Long> rm = roleManageService.updateEnableMark(roleEnableMarkRequest.getEnableMark(),roleEnableMarkRequest.getId());
        return rm;
    }

    @ApiOperation(value = "测试获取角色详情请求", notes = "获取角色详情接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    @RequestMapping(value = "/role/detail/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseMessage<RoleDetail> getRoleDetail(@ApiParam(value = "角色ID", required = true) @PathVariable Long id){
        ResponseMessage<RoleDetail> rm = roleManageService.queryRoleDetail(id);
        return rm;
    }
    @ApiOperation(value = "测试获取新增页面信息请求", notes = "获取新增页面信息接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    @RequestMapping(value = "/role/info/insert", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseMessage<RoleInsertInfo> getRoleInsertInfo(){
        ResponseMessage<RoleInsertInfo> rm = roleManageService.queryRoleInsertInfo();
        return rm;
    }

    @ApiOperation(value = "测试复制角色请求", notes = "复制角色接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "正常"),
            @ApiResponse(code = 10001, message = "系统错误"),
            @ApiResponse(code = 20002, message = "参数错误")
    })
    @RequestMapping(value = "/role/copy/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseMessage<Long> copyRole(@ApiParam(value = "角色ID", required = true) @PathVariable Long id){
        ResponseMessage<Long> rm = roleManageService.copyRole(id);
        return rm;
    }

}